package aqu.persistencia.utilidad;

import aqu.controlador.LoginController;
import aqu.persistencia.Dao.Administracion.SegUsuarioManager;
import aqu.persistencia.entity.SegUsuario;
import java.util.Iterator;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@ManagedBean(name = "tree")
@SessionScoped
public class TreeBean {

    private DefaultTreeModel model;
    private UrlNodeUserObject selectedNodeObject = null;
    private SegUsuarioManager segUsuarioManager = new SegUsuarioManager();
    private DefaultMutableTreeNode rootTreeNode;
    private SegUsuario usuario;

    public TreeBean() {


        //LoginController lc=(LoginController)session.getAttribute("usuario");

        /*HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
        .getExternalContext().getSession(false);
        HttpServletResponse rp = (HttpServletResponse) FacesContext
        .getCurrentInstance().getExternalContext().getResponse();
        HttpServletRequest rq = (HttpServletRequest) FacesContext
        .getCurrentInstance().getExternalContext().getRequest();*/
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        LoginController lc = (LoginController) session.getAttribute("loginController");
        usuario = lc.getUsuario();
        ////////////////////////////////
        // create root node with its children expanded
        rootTreeNode = new DefaultMutableTreeNode();
        UrlNodeUserObject rootObject = new UrlNodeUserObject(rootTreeNode, this);
        rootObject.setText("Menu");
        rootObject.setUrl("");
        rootObject.setExpanded(true);
        rootTreeNode.setUserObject(rootObject);

        // model is accessed by by the ice:tree component
        model = new DefaultTreeModel(rootTreeNode);
        LlenarMenu("-", null);

    }

    public DefaultTreeModel getModel() {
        return model;
    }

    public void checkChanged(ValueChangeEvent event) {
        System.out.println("checkboxed selected " + event.getComponent().getId());
    }

    public void setSelectedNodeObject(UrlNodeUserObject selectedNodeObject) {
        this.selectedNodeObject = selectedNodeObject;

    }

    public UrlNodeUserObject getSelectedNodeObject() {
        return selectedNodeObject;
    }

    public String nodeC(ActionEvent event) {


        return "contentP.xhtml";
    }

    public String onTreeNodeClicked() {
        return getSelectedNodeObject().getUrl();

    }

    public void LlenarMenu(String padre, DefaultMutableTreeNode TreeNode) {
        Iterator results = segUsuarioManager.buscarConSQLQuery("SELECT u.SEG_LOGIN_USU,p.SEG_NOMBRE_PER,m.SEG_CODIGO_MEN,m.SEG_MENUPADRE_MEN,m.SEG_DESCRIPCION_MEN,IFNULL(m.SEG_VENTANA_MEN,'') "
                + " FROM seg_usuario u,seg_perfil p,seg_perfil_menu s, seg_menu m "
                + " where p.SEG_CODIGO_PER=u.SEG_CODIGO_PER "
                + " and p.SEG_CODIGO_PER=s.SEG_CODIGO_PER "
                + " and s.SEG_CODIGO_MEN=m.SEG_CODIGO_MEN "
                + " and m.SEG_MENUPADRE_MEN='" + padre + "'"
                + " and u.SEG_LOGIN_USU='"+usuario.getSegLoginUsu()+"' "
                + " order by SEG_DESCRIPCION_MEN asc");

        while (results.hasNext()) {
            Object[] row = (Object[]) results.next();
            System.out.println(row[0]);
            System.out.println(row[1]);
            System.out.println(row[2]);
            System.out.println(row[3]);
            System.out.println(row[4]);

            DefaultMutableTreeNode branchNode = new DefaultMutableTreeNode();
            UrlNodeUserObject branchObject = new UrlNodeUserObject(branchNode, this);
            branchObject.setText(row[4].toString());
            branchObject.setUrl(row[5].toString());
            if (!row[5].toString().trim().equals("")) {
                branchObject.setLeaf(true);
            }
            branchNode.setUserObject(branchObject);

            if (row[3].equals("-")) {
                rootTreeNode.add(branchNode);


            } else {
                //a_treeh.getChildren().add(treeh);
                TreeNode.add(branchNode);
            }
            LlenarMenu(row[2].toString(), branchNode);
        }


    }
}
